<?php

namespace app\common\service;

use app\model\Config;
use app\common\AppService;

/**
 * Class ConfigService
 * @package app\service
 */
class ConfigService extends AppService
{
    /**
     * 获取配置
     * @param $key
     * @param string $addon
     */
    public static function getConf($key, $addon = 'admin')
    {
        $conf = Config::getOne(['addon' => $addon, 'k' => $key]);
        if (empty($conf))
        {
            return [];
        }
        $conf = json_decode($conf['v'], true);
        return $conf;
    }

    /**
     * 保存配置
     * @param $data
     * @param string $addon
     */
    public static function setConf($key, $addon = 'admin', $data, $desc = '')
    {
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $conf = Config::getOne(['addon' => $addon, 'k' => $key]);
        if (empty($conf))
        {
            $saveData['k'] = $key;
            $saveData['v'] = $data;
            $saveData['addon'] = $addon;
            $saveData['desc'] = $desc;
            Config::addOne($saveData);
        } else {
            Config::updateOne(['v' => $data], ['addon' => $addon, 'k' => $key]);
        }
    }

    /**
     * @desc 获取当前系统的上传方式
     * @return mixed
     */
    public static function getUploadType()
    {
        $systemUploadConfig = config('system.upload');
        return $systemUploadConfig['type'];
    }
}